
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>浏览页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body class="pear-container">
        <style>
            .ico {
                height: 20px;
                width: 22px;
                margin-right: 10px;
                position: relative;
                top: 2px;
                display: inline-block;
                background-size: 100% 100%;
            }
            .ico-json {
                background-image: url('')
            }

            .ico-folder {
                background-image: url("")
            }

            .ico-folder-unempty {
                background-image: url("")
            }

            .ico-access,
            .ico-mdb,
            .ico-accdb,
            .ico-db,
            .ico-sql {
                background-image: url("")
            }

            .ico-c {
                background-image: url("")
            }

            .ico-cpp {
                background-image: url("")
            }

            .ico-cs {
                background-image: url("")
            }

            .ico-fla,
            .ico-flv {
                background-image: url("")
            }

            .ico-css {
                background-image: url("")
            }

            .ico-js {
                background-image: url("")
            }

            .ico-htm {
                background-image: url("")
            }

            .ico-html {
                background-image: url("")
            }

            .ico-java {
                background-image: url("")
            }

            .ico-log {
                background-image: url("")
            }

            .ico-mht {
                background-image: url("")
            }

            .ico-php {
                background-image: url("")
            }

            .ico-url {
                background-image: url("")
            }

            .ico-xml {
                background-image: url("")
            }

            .ico-ai {
                background-image: url("")
            }

            .ico-bmp {
                background-image: url("")
            }

            .ico-cdr {
                background-image: url("")
            }

            .ico-gif {
                background-image: url("")
            }

            .ico-ico {
                background-image: url("")
            }

            .ico-jpeg {
                background-image: url("")
            }

            .ico-jpg,
            .ico-JPG {
                background-image: url("")
            }

            .ico-png {
                background-image: url("")
            }

            .ico-psd {
                background-image: url("")
            }

            .ico-webp {
                background-image: url("")
            }

            .ico-ape {
                background-image: url("")
            }

            .ico-avi {
                background-image: url("")
            }

            .ico-flv {
                background-image: url("")
            }

            .ico-mkv {
                background-image: url("")
            }

            .ico-mov {
                background-image: url("")
            }

            .ico-mp3 {
                background-image: url("")
            }

            .ico-mp4 {
                background-image: url("")
            }

            .ico-mpeg {
                background-image: url("")
            }

            .ico-mpg {
                background-image: url("")
            }

            .ico-rm {
                background-image: url("")
            }

            .ico-rmvb {
                background-image: url("")
            }

            .ico-swf {
                background-image: url("")
            }

            .ico-wav {
                background-image: url("")
            }

            .ico-webm {
                background-image: url("")
            }

            .ico-wma {
                background-image: url("")
            }

            .ico-wmv {
                background-image: url("")
            }

            .ico-doc,
            .ico-docm,
            .ico-dotx,
            .ico-dotm,
            .ico-dot,
            .ico-rtf {
                background-image: url("")
            }

            .ico-docx {
                background-image: url("")
            }

            .ico-pdf,
            .ico-fdf {
                background-image: url("")
            }

            .ico-ppt,
            .ico-pptm,
            .ico-pot,
            .ico-potm {
                background-image: url("")
            }

            .ico-pptx {
                background-image: url("")
            }

            .ico-txt {
                background-image: url("")
            }

            .ico-xls,
            .ico-csv,
            .ico-xlsm,
            .ico-xlsb {
                background-image: url("")
            }

            .ico-xlsx {
                background-image: url("")
            }

            .ico-7z {
                background-image: url("")
            }

            .ico-cab {
                background-image: url("")
            }

            .ico-iso {
                background-image: url("")
            }

            .ico-rar {
                background-image: url("")
            }

            .ico-zip {
                background-image: url("")
            }

            .ico-gz,
            .ico-bz2 {
                background-image: url("")
            }

            .ico-bt {
                background-image: url("")
            }

            .ico-file {
                background-image: url("")
            }

            .ico-apk {
                background-image: url("")
            }
            .ico-file {
                background-image: url("")
            }
        </style>
    
        <!-- 顶部查询表单 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form top-search-from">

                    <input type="hidden" name="pid" value="0" />

                    <div class="layui-form-item">
                        <label class="layui-form-label">名称</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block">
<!--                                <input type="hidden" autocomplete="off" name="title[]" value="like" class="layui-input inline-block">-->
                                <input type="text" autocomplete="off" name="title" class="layui-input">
                            </div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">创建时间</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block" id="created_at">
                                <input type="text" autocomplete="off" name="created_at[]" id="created_at-date-start" class="layui-input inline-block" placeholder="开始时间">
                                -
                                <input type="text" autocomplete="off" name="created_at[]" id="created_at-date-end" class="layui-input inline-block" placeholder="结束时间">
                            </div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label"></label>
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query" id="searchBtn">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="table-reset">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>
                    </div>
                    <div class="toggle-btn">
                        <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                        <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- 数据表格 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
                </span>
                <table id="data-table" lay-filter="data-table"></table>
            </div>
        </div>

        <script type="text/html" id="table-breadcrumb">
            <a href="javascript:;" data-id="0">当前位置：</a><span lay-separator="">/</span>
            {{#  layui.each(d.path, function(index, item){ }}
                <a href="javascript:;" data-id="{{= d.pids[index] }}">{{= item }}</a><span lay-separator="">/</span>
            {{#  }); }}
        </script>

        <!-- 表格顶部工具栏 -->
        <script type="text/html" id="table-toolbar">
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" permission="app.netdisk.admin.iosource.insert">
                <i class="layui-icon layui-icon-add-1"></i>新建文件夹
            </button>
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="addFile" permission="app.netdisk.admin.iosource.insert">
                <i class="layui-icon layui-icon-add-1"></i>上传文件
            </button>
        </script>

        <!-- 表格行工具栏 -->
        <script type="text/html" id="table-bar">
            {{# if(d.is_folder == 1){ }}<button class="pear-btn pear-btn-xs tool-btn" lay-event="open">打开</button>{{# } }}
<!--            <button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="app.netdisk.admin.iosource.update">编辑</button>-->
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="share">分享</button>
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="app.netdisk.admin.iosource.delete">删除</button>
        </script>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/common.js"></script>
        
        <script>

            // 相关常量
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/netdisk/admin/io-source/select";
            const UPDATE_API = "/app/netdisk/admin/io-source/update";
            const DELETE_API = "/app/netdisk/admin/io-source/delete";
            const INSERT_URL = "/app/netdisk/admin/io-source/insert";
            const UPDATE_URL = "/app/netdisk/admin/io-source/update";
            const UPDATE_NAME_API = "/app/netdisk/admin/io-source/rename";
            const UPLOAD_URL = "/app/netdisk/admin/io-source/upload";
            const SHARE_URL = "/app/netdisk/admin/io-source/share";

            // 字段 创建时间 created_at
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#created_at",
                    range: ["#created_at-date-start", "#created_at-date-end"],
                    type: "datetime",
                });
            })
            
            // 表格渲染
            layui.use(["table", "form", "common", "popup", "util", "laytpl"], function() {
                let table = layui.table;
                let form = layui.form;
                let $ = layui.$;
                let common = layui.common;
                let util = layui.util;
                let laytpl = layui.laytpl;
                let where = {
                    field: "id",
                    order: "desc",
                    pid:0,
                };
                
				// 表头参数
				let cols = [
					{
						type: "checkbox",
						align: "center"
					},{
						title: "主键",align: "center",
						field: "id",
						sort: true,
                        width:80,
					},{
						title: "名称",align: "center",
						field: "title",
                        edit:true,
                        align:'left',
                        templet: function (d) {
                            return "<i class='ico ico-"+(d.is_folder!=1?d.ext:'folder')+"'></i>"+d.title;
                        }
					},{
						title: "扩展名",align: "center",
						field: "ext",
                        hide: true,
					},{
						title: "文件大小",align: "center",
						field: "file_size",
                        width: 180,
                        templet: function (d) {
                            return !d.is_folder?formatSize(d.file_size):'-';
                        }
					},{
						title: "创建时间",align: "center",
						field: "created_at",
						sort: true,
                        width: 180,
					},{
						title: "更新时间",align: "center",
						field: "updated_at",
                        width: 180,
					},{
						title: "操作",
						toolbar: "#table-bar",
						align: "center",
						fixed: "right",
						width: 200,
					}
				];
				
				// 渲染表格
				table.render({
				    elem: "#data-table",
				    url: SELECT_API,
				    page: true,
				    cols: [cols],
				    overflow:'tips',
				    skin: "line",
				    size: "lg",
				    toolbar: "#table-toolbar",
				    autoSort: false,
				    where: where,
                    page:false,
                    limit:1000,
				    defaultToolbar: [{
				        title: "刷新",
				        layEvent: "refresh",
				        icon: "layui-icon-refresh",
				    }],
				    done: function (data) {
                        laytpl($("#table-breadcrumb").html()).render(data, function(str){
                            $(".layui-breadcrumb").html(str);
                        });
				        layer.photos({photos: 'div[lay-id="data-table"]', anim: 5});

                        //当前位置切换事件
                        $(".layui-breadcrumb a").click(function (){
                            value = $(this).attr('data-id');
                            $("input[name='pid']").val(value);

                            $('#searchBtn').click();
                        });
				    }
				});

				
                // 编辑或删除行事件
                table.on("tool(data-table)", function(obj) {
                    if (obj.event === "remove") {
                        remove(obj);
                    } else if (obj.event === "edit") {
                        edit(obj);
                    } else if (obj.event === "open") {
                        open(obj);
                    } else if (obj.event === "share") {
                        share(obj);
                    }
                });

                // 表格顶部工具栏事件
                table.on("toolbar(data-table)", function(obj) {
                    if (obj.event === "add") {
                        add();
                    } else if (obj.event === "addFile") {
                        addFile();
                    } else if (obj.event === "refresh") {
                        refreshTable();
                    } else if (obj.event === "batchRemove") {
                        batchRemove(obj);
                    }
                });

                // 表格顶部搜索事件
                form.on("submit(table-query)", function(data) {
                    where = Object.assign(where, data.field);
                    table.reload("data-table", {
                        where: where
                    })
                    return false;
                });
                
                // 表格顶部搜索重置事件
                form.on("submit(table-reset)", function(data) {
                    where = {
                        field: 'id',
                        order: 'desc',
                        pid:0
                    };
                    table.reload("data-table", {
                        where: where
                    })
                });
                
                // 字段允许为空
                form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });

                // 表格排序事件
                table.on("sort(data-table)", function(obj){
                    var sort = {
                        field: obj.field,
                        order: obj.type
                    };
                    where = Object.assign(where, sort);
                    table.reload("data-table", {
                        initSort: obj,
                        scrollPos: "fixed",
                        where: where
                    });
                });

                //监听单元格编辑
                table.on('edit(data-table)', function(obj){
                    let pid = $("input[name='pid']").val();
                    var value = obj.value //得到修改后的值
                        ,data = obj.data //得到所在行所有键值
                        ,oldValue = obj.oldValue //得到修改前的值
                        ,field = obj.field; //得到字段
                    let postData = {};
                    postData[field] = util.escape(value);
                    postData[PRIMARY_KEY] = data.id;
                    postData['oldName'] = oldValue;
                    postData['pid'] = pid;
                    $.ajax({
                        url: UPDATE_NAME_API,
                        type: "POST",
                        dateType: "json",
                        data: postData,
                        success: function (res) {
                            if (res.code) {
                                refreshTable();
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功");
                        }
                    });
                });

                // 表格新增数据
                let add = function() {
                    let pid = $("input[name='pid']").val();
                    layer.open({
                        type: 2,
                        title: "新建文件夹",
                        shade: 0.1,
                        shadeClose:true,
                        area: [common.isModile()?"100%":"45%", common.isModile()?"100%":"45%"],
                        content: INSERT_URL+"?pid="+pid
                    });
                }

                // 上传文件
                let addFile = function() {
                    let pid = $("input[name='pid']").val();
                    layer.open({
                        type: 2,
                        title: "上传文件",
                        shade: 0.1,
                        shadeClose:true,
                        area: [common.isModile()?"100%":"80%", common.isModile()?"100%":"60%"],
                        content: UPLOAD_URL+"?pid="+pid
                    });
                }

                //打开下级文件
                let open = function(obj){
                    let value = obj.data[PRIMARY_KEY];
                    $("input[name='pid']").val(value);

                    $('#searchBtn').click();
                }

                //分享
                let share = function(obj){
                    let id = obj.data[PRIMARY_KEY];
                    let title = obj.data['title'];
                    shareFrame(id,title);
                }
                //分享弹窗
                window.shareFrame = function(id,title){
                    layer.open({
                        type: 2,
                        title: "设置分享文件夹-["+title+"]",
                        shade: 0.1,
                        shadeClose:true,
                        area: [common.isModile()?"100%":"45%", common.isModile()?"100%":"45%"],
                        content: SHARE_URL+"?id="+id
                    });
                }

                // 表格编辑数据
                let edit = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "修改文件夹",
                        shade: 0.1,
                        shadeClose:true,
                        area: [common.isModile()?"100%":"45%", common.isModile()?"100%":"45%"],
                        content: UPDATE_URL + "?" + PRIMARY_KEY + "=" + value
                    });
                }

                // 删除一行
                let remove = function(obj) {
                    return doRemove(obj.data[PRIMARY_KEY]);
                }

                // 删除多行
                let batchRemove = function(obj) {
                    let checkIds = common.checkField(obj, PRIMARY_KEY);
                    if (checkIds === "") {
                        layui.popup.warning("未选中数据");
                        return false;
                    }
                    doRemove(checkIds.split(","));
                }

                // 执行删除
                let doRemove = function (ids) {
                    let data = {};
                    data[PRIMARY_KEY] = ids;
                    layer.confirm("确定删除?", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: DELETE_API,
                            data: data,
                            dataType: "json",
                            type: "post",
                            success: function(res) {
                                layer.close(loading);
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("操作成功", refreshTable);
                            }
                        })
                    });
                }

                // 刷新表格数据
                window.refreshTable = function(param) {
                    table.reloadData("data-table", {
                        scrollPos: "fixed"
                    });
                }

                // 格式化文件大小
                let formatSize = function(value) {
                    if(null == value || "" === value){
                        return "0 B";
                    }
                    let unitArr = ["B","KB","MB","GB","TB","PB","EB","ZB","YB"];
                    let index = 0;
                    let srcSize = parseFloat(value);
                    index = Math.floor(Math.log(srcSize) / Math.log(1024));
                    let size =srcSize / Math.pow(1024, index);
                    size = size.toFixed(2);
                    return size + unitArr[index];
                }
            })

        </script>
    </body>
</html>
